package com.birdy.util;

import com.birdy.annotation.LogTimeUsed;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author birdy
 */
@Slf4j
public class HttpSender {
    @LogTimeUsed
    public String get(String url) throws Exception {
        BufferedReader in;
        StringBuilder sb = new StringBuilder();
        URL realUrl = new URL(url);
        URLConnection conn = realUrl.openConnection();
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//        conn.setConnectTimeout(500);
//        conn.setReadTimeout(1000);
        conn.connect();
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine())!= null) {
            sb.append("\n").append(line);
        }
        try {
            in.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return sb.toString();
    }
}
